package com.uzai.console.service.mini.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.BaseDto;
import com.uzai.console.dto.mini.base.MiniPageConfigDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.mini.MiniBaseService;
import com.uzai.console.vo.mini.base.MiniPageConfigVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class MiniBaseServiceImpl implements MiniBaseService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询微信小程序页面设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public MiniPageConfigVo findMiniPageConfig(BaseDto baseDto) {

        MiniPageConfigVo miniPageConfigVo = new MiniPageConfigVo();

        //查询运营商微信小程序页面设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(baseDto.getMerId(), MerConfigKeyEnum.MINI_PAGE_CONFIG.getValue());
        if (merBaseConfig != null) {
            miniPageConfigVo = JSONObject.parseObject(merBaseConfig.getVal(), MiniPageConfigVo.class);
        }
        return miniPageConfigVo;
    }

    /**
     * 修改微信小程序页面设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param miniPageConfigDto
     * @return null
     */
    public void updateMiniPageConfig(MiniPageConfigDto miniPageConfigDto){

        MiniPageConfigVo miniPageConfigVo = new MiniPageConfigVo();
        BeanUtils.copyProperties(miniPageConfigDto, miniPageConfigVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(miniPageConfigDto.getMerId(), MerConfigKeyEnum.MINI_PAGE_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(miniPageConfigVo)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(miniPageConfigDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.DEVICES.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.MINI_PAGE_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(miniPageConfigVo)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.MINI_PAGE_CONFIG.getKey(), miniPageConfigDto.getMerId()), merBaseConfig.getVal());
    }

}
